{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用Python破解ZIP或RAR压缩文件密码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 库\n",
    "- zipfile：Python 标准库，使用时直接导入即可\n",
    "- rarfile：Python 第三方库，使用时需要安装，API 文档(https://rarfile.readthedocs.io/en/latest/api.html)\n",
    "\n",
    "\n",
    "- itertools：Python 标准库，无限迭代器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 补充：[Linux下zip加密压缩](https://www.cnblogs.com/zfying/archive/2012/07/07/2580467.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "zip -rP abc#123 test.zip test.txt\n",
    "\n",
    "-P abc#123 是加密密码\n",
    "-r 递归，将指定目录下的所有文件和子目录一并处理\n",
    "test.zip 是生成的压缩文件\n",
    "test.txt 是被压缩的文件\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python生成密码本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T21:20:31.861714Z",
     "start_time": "2020-08-20T21:20:31.854455Z"
    }
   },
   "outputs": [],
   "source": [
    "import itertools\n",
    "import zipfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T21:14:40.087830Z",
     "start_time": "2020-08-20T21:14:40.082612Z"
    }
   },
   "outputs": [],
   "source": [
    "def permutation(li):\n",
    "    print(list(itertools.permutations(li)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T21:14:53.119772Z",
     "start_time": "2020-08-20T21:14:53.114324Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]\n"
     ]
    }
   ],
   "source": [
    "permutation(\"abc\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 密码本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T21:17:40.185737Z",
     "start_time": "2020-08-20T21:17:40.182111Z"
    }
   },
   "outputs": [],
   "source": [
    "words = \"abc123\"\n",
    "r = itertools.product(words, repeat = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-20T21:17:45.662594Z",
     "start_time": "2020-08-20T21:17:45.655660Z"
    }
   },
   "outputs": [],
   "source": [
    "dic = open(\"pass.txt\", \"a\")\n",
    "for i in r:\n",
    "    dic.write(\"\".join(i))\n",
    "    dic.write(\"\".join(\"\\r\"))\n",
    "dic.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "未完成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-21T03:31:04.708143Z",
     "start_time": "2020-08-21T03:31:04.700390Z"
    }
   },
   "outputs": [],
   "source": [
    "def unzip():\n",
    "    filename = \"test.zip\"\n",
    "    fp = zipfile.ZipFile(filename)\n",
    "    try:\n",
    "        fpPwd = open('pass.txt')\n",
    "    except:\n",
    "        print(\"破解失败，密码不匹配\")\n",
    "    for pwd in fpPwd:\n",
    "        pwd = pwd.rstrip()\n",
    "        try:\n",
    "            fp.extractall(path = desPath, pwd = pwd.eccode())\n",
    "            print(\"成功\"+pwd)\n",
    "            fp.close()\n",
    "            break\n",
    "        except:\n",
    "            pass\n",
    "\n",
    "    fpPwd.close()\n",
    "    fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-21T03:31:07.240706Z",
     "start_time": "2020-08-21T03:31:07.234827Z"
    }
   },
   "outputs": [],
   "source": [
    "unzip()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
